#include "gamescene.h"
#include "item/chessboarditem.h"
#include "commondef.h"
#include "item/backbtnitem.h"
#include "item/scoreitem.h"
#include "item/numberbtnitem.h"
#include "global/myresource.h"
#include "global/globalvar.h"
#include <QGraphicsSceneMouseEvent>
#include "item/currenttimeitem.h"
#include "item/timeitem.h"
#include "item/promptbtnitem.h"
#include <QTimer>
#include "item/erroritem.h"
#include "item/remainitem.h"
#include "item/menoitem.h"
#include <QDebug>

gamescene::gamescene(QObject * parent):
    QGraphicsScene              (parent)
{
    setSceneRect(0.0, 0.0, WINDOW_WIDTH,WINDOW_HEIGHT);

    initItem();
    initNumItem();
}

gamescene::~gamescene()
{

}

void gamescene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

    if(m_backItem->isUnderMouse())
    {
        if(globalvar::m_bVoice)
        {
            MyResource::play(1);
        }
        globalvar::m_logic.saveChess();

        endGame();

        emit gameback();
    }
    else if(m_aboutBtnItem->isUnderMouse())
    {
        if(globalvar::m_bVoice)
        {
            MyResource::play(1);
        }
        pause();
    }
    else if(m_memoBtnItem->isUnderMouse())
    {
        if(globalvar::m_bVoice)
        {
            MyResource::play(1);
        }

        globalvar::m_logic.MemoChange();
    }
    else if(m_cbItem->isUnderMouse())
    {
        if(globalvar::m_bVoice)
        {
            MyResource::play();
        }
        globalvar::m_logic.managePos(m_cbItem->mapFromScene(event->scenePos()));

    }
    else if(m_promptBtnItem->isUnderMouse())
    {
        if(globalvar::m_logic.m_bPromptBtnEnable)
        {
            if(globalvar::m_bVoice)
            {
                MyResource::play(1);
            }
            globalvar::m_logic.BeginPrompt();
            QTimer::singleShot(2000,this,SLOT(endShowPrompt()));
        }
    }
    else if(globalvar::m_logic.m_numtparr[globalvar::m_logic.m_oldsel].bdig)
    {
        for(int i = 0; i < m_numItemarr.count();++i)
        {
            if(m_numItemarr[i]->isUnderMouse())
            {
                if(globalvar::m_bVoice)
                {
                    MyResource::play(1);
                }
                globalvar::m_logic.manageNumberBtnPos(i + 1);
                break;
            }
        }
    }
}

void gamescene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if(globalvar::m_logic.m_iselbtn != -1)
    {
       globalvar::m_logic.m_iselbtn = -1;
    }
}

void gamescene::initItem()
{
    m_cbItem = new ChessBoardItem();
    m_cbItem->setPos(0.0f,H1);
    addItem(m_cbItem);

    m_backItem = new BackBtnItem();
    m_backItem->setPos(0.0f,H5);
    addItem(m_backItem);

//    m_scoreItem = new ScoreItem();
//    m_scoreItem->setPos(0.0f,0.0f);
//    addItem(m_scoreItem);

    m_aboutBtnItem = new QGraphicsPixmapItem(QPixmap::fromImage(*MyResource::m_gameaboutbtnImage));
    m_aboutBtnItem->setPos(162.0f,0.0f);
    addItem(m_aboutBtnItem);

    m_remainItem = new RemainItem();
    m_remainItem->setPos(240.0f,H2);
    addItem(m_remainItem);

    m_promptBtnItem = new PromptBtnItem();
    m_promptBtnItem->setPos(300.0f,H5);
    addItem(m_promptBtnItem);

    m_errorItem = new ErrorItem();
    m_errorItem->setPos(0.0f,H2);
    addItem(m_errorItem);

    m_timeItem = new TimeItem();
    m_timeItem->setPos(120.0f,H2);
    addItem(m_timeItem);
    m_timeItem->beginTime();
    globalvar::m_timeitem = m_timeItem;

    m_memoBtnItem = new MenoItem();
    m_memoBtnItem->setPos(120.0f,H5);
    addItem(m_memoBtnItem);

    m_curTimeItem = new CurrentTimeItem();
    m_curTimeItem->setPos(300.0f,0.0f);
    addItem(m_curTimeItem);
}

void gamescene::initNumItem()
{
    for(int i = 1; i < 6; ++i)
    {
        NumberBtnItem *numitem = new NumberBtnItem(i);
        numitem->setPos(72 * (i - 1) + 1,H3);
        addItem(numitem);
        m_numItemarr.append(numitem);
    }

    for(int i = 6; i < 10; ++i)
    {
        NumberBtnItem *numitem = new NumberBtnItem(i);
        numitem->setPos(72 * (i - 6) + 37,H4);
        addItem(numitem);
        m_numItemarr.append(numitem);
    }
}

void gamescene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(m_cbItem->isUnderMouse())
    {
        globalvar::m_logic.managePos(m_cbItem->mapFromScene(event->scenePos()));
    }
    else if(globalvar::m_logic.m_numtparr[globalvar::m_logic.m_oldsel].bdig)
    {
        for(int i = 0; i < m_numItemarr.count();++i)
        {
            if(m_numItemarr[i]->isUnderMouse())
            {
                globalvar::m_logic.manageNumberBtnPos(i + 1);
                break;
            }
        }
    }
}

void gamescene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if(m_cbItem->isUnderMouse())
    {
        globalvar::m_logic.MemoChange();
    }
}

void gamescene::endGame()
{
    m_timeItem->m_time->stop();
}

void gamescene::initGame(bool bcontinue)
{
    m_timeItem->beginTime(bcontinue);
}

void gamescene::endShowPrompt()
{
    globalvar::m_logic.EndPrompt();
}

void gamescene::win()
{

    endGame();

    emit gameback();
}

void gamescene::continueGame()
{
    m_timeItem->m_time->start();
}

void gamescene::pause()
{
    endGame();

    emit showhelp();
}


